package com.great.controller;

import com.github.pagehelper.PageInfo;
import com.great.domain.Cate;
import com.great.domain.Product;
import com.great.service.IndexService;
import com.great.service.ProductService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@Transactional
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    @Autowired
    private IndexService indexService;

    @RequestMapping("/findAllProduct")
    public ModelAndView findAllProduct(@RequestParam(name = "page", required = true,defaultValue = "1") int page, @RequestParam(name = "size",required = true,defaultValue = "5") int size){
        ModelAndView mv = new ModelAndView();
        List<Product> product = productService.findAllProduct(page,size);
        PageInfo pageInfo = new PageInfo(product);
        List<Cate> flist = indexService.findAllParentCate();
        mv.addObject("flist",flist);
        List<Cate> clist = indexService.findAllChildCate();
        mv.addObject("clist",clist);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("flist",flist);
        mv.addObject("clist",clist);
        mv.setViewName("manage/product-list");
        return mv;
    }

    @RequestMapping("/productadd")
    public ModelAndView productadd(){
        ModelAndView mv = new ModelAndView("manage/product-add");
        List<Cate> flist = indexService.findAllParentCate();
        mv.addObject("flist",flist);
        List<Cate> clist = indexService.findAllChildCate();
        mv.addObject("clist",clist);
        return mv;
    }

    @RequestMapping("/add")
    public String updateGoods(@RequestParam(value="product_image1")MultipartFile pictureFile,HttpServletRequest request,Product product) throws IOException{
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        String url = request.getSession().getServletContext().getRealPath("/img/product");
        pictureFile.transferTo(new File(url + "/" + name + ".jpg" ));

        product.setProduct_image(name+".jpg");
        String id = request.getParameter("parentid");
        product.setProduct_fid(Integer.parseInt(id.split("-")[0]));
        product.setProduct_cid(Integer.parseInt(id.split("-")[1]));
        productService.addproduct(product);
        return "redirect:/product/findAllProduct";
    }

    @RequestMapping("findproductById")
    public ModelAndView findproductById(Integer product_id){
        ModelAndView mv = new ModelAndView("manage/product-change");
        List<Cate> flist = indexService.findAllParentCate();
        mv.addObject("flist",flist);
        List<Cate> clist = indexService.findAllChildCate();
        mv.addObject("clist",clist);
        Product product = productService.findproductById(product_id);
        mv.addObject("product",product);
        return mv;
    }

    @RequestMapping("productchange")
    public String productchange(@RequestParam(value="product_image1")MultipartFile pictureFile,HttpServletRequest request,Product product) throws IOException{
        if(pictureFile.isEmpty()){

        }else{
            String name = UUID.randomUUID().toString().replaceAll("-", "");
            String url = request.getSession().getServletContext().getRealPath("/img/product");
            pictureFile.transferTo(new File(url + "/" + name + ".jpg" ));
            product.setProduct_image(name+".jpg");
        }
        String id = request.getParameter("parentid");
        product.setProduct_fid(Integer.parseInt(id.split("-")[0]));
        product.setProduct_cid(Integer.parseInt(id.split("-")[1]));
        productService.productchange(product);
        return "redirect:/product/findAllProduct";
    }

    @RequestMapping("/xiajia")
    public String xiajia(String tag){
        String[] strs = tag.split(",");
        for(int i=0;i<strs.length;i++){
            productService.xiajia(Integer.parseInt(strs[i]));
        }
        return "redirect:/product/findAllProduct";
    }

    @RequestMapping("/shangjia")
    public String shangjia(String tag){
        String[] strs = tag.split(",");
        for(int i=0;i<strs.length;i++){
            productService.shangjia(Integer.parseInt(strs[i]));
        }
        return "redirect:/product/findAllProduct";
    }


    @RequestMapping("/findProduct")
    public ModelAndView findcheProduct(@RequestParam(name = "page", required = true,defaultValue = "1") int page,
                                       @RequestParam(name = "size",required = true,defaultValue = "5") int size,
                                       Product product,
                                       @RequestParam(name="qrycidType") String qrycidType,
                                       @RequestParam(name="qryfidType") String qryfidType,
                                       @RequestParam(name="qrystatus") String qrystatus,
                                       @RequestParam(name="qrysearch") String qrysearch){
        product.setProduct_fid(-1);
        product.setProduct_cid(-1);
        product.setProduct_status(-1);
        if(!StringUtils.isNullOrEmpty(qrycidType)){
            product.setProduct_cid(Integer.parseInt(qrycidType));
        }
        if (!StringUtils.isNullOrEmpty(qryfidType)){
            product.setProduct_fid(Integer.parseInt(qryfidType));}
        if (!StringUtils.isNullOrEmpty(qrystatus)){
            product.setProduct_status(Integer.parseInt(qrystatus));}
        ModelAndView mv = new ModelAndView();
        List<Product> pro = productService.findcheProduct(page,size,product,qrysearch);
        PageInfo pageInfo = new PageInfo(pro);
        mv.addObject("pageInfo",pageInfo);
        List<Cate> flist = indexService.findAllParentCate();
        mv.addObject("flist",flist);
        List<Cate> clist = indexService.findAllChildCate();
        mv.addObject("clist",clist);
        mv.setViewName("manage/product-list");
        return mv;
    }
}
